﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.CycleTimeReport.CApCTRUtilities
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Reflection;
using System.Resources;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.Olp.PrivateImplementationDetails;


namespace DnProcessSimulateCommands.CycleTimeReport;

internal class CApCTRUtilities
{
  public const string DEFAULT_DOUBLE_PRECISION = "f3";
  public const string REGISTRY_IGNORE_USER_TIMERS = "Ignore user timers";
  public const string REGISTRY_IGNORE_TIMER_PRIORITY = "Ignore timer priority";
  private static ResourceManager m_resourceManager;
  private static ITxCommand m_command;
  private static TxCommandPrivateDataStorage m_registryManager;

  public static void InitRegistryManager(ITxCommand command)
  {
    CApCTRUtilities.m_command = command;
    CApCTRUtilities.m_registryManager = new TxCommandPrivateDataStorage(CApCTRUtilities.m_command);
  }

  private static int? LoadRegistryIntValue(string valueName)
  {
    int? nullable = new int?();
    try
    {
      if (CApCTRUtilities.m_registryManager != null)
        nullable = new int?(CApCTRUtilities.m_registryManager.GetIntValue(valueName));
    }
    catch (TxCommandPrivateDataValueNotStoredException ex)
    {
      nullable = new int?();
    }
    return nullable;
  }

  public static bool IntToBool(int x) => x == 1;

  public static int BoolToInt(bool val) => val ? 1 : 0;

  public static bool GetBoolValueFromRegistry(string valueName, bool defaultValue)
  {
    bool valueFromRegistry = defaultValue;
    int? nullable = CApCTRUtilities.LoadRegistryIntValue(valueName);
    if (nullable.HasValue)
      valueFromRegistry = CApCTRUtilities.IntToBool(nullable.Value);
    return valueFromRegistry;
  }

  public static void SetBoolValueToRegistry(string valueName, bool theValue)
  {
    CApCTRUtilities.m_registryManager.SetIntValue(valueName, CApCTRUtilities.BoolToInt(theValue));
  }

  private static string LoadRegistryStringValue(string valueName)
  {
    string str = (string) null;
    try
    {
      if (CApCTRUtilities.m_registryManager != null)
        str = CApCTRUtilities.m_registryManager.GetStringValue(valueName);
    }
    catch (TxCommandPrivateDataValueNotStoredException ex)
    {
      str = (string) null;
    }
    return str;
  }

  public static void UpdateSettingsTimerInfoFromRegistry(CApCTRSettingsTimerInfo timerSetting)
  {
    string str = CApCTRUtilities.LoadRegistryStringValue(timerSetting.TimerName);
    if (str == null)
      return;
    timerSetting.SetFromString(str);
  }

  public static void SetSettingsTimerInfoToRegistry(
    string valueName,
    CApCTRSettingsTimerInfo theValue)
  {
    string str = theValue.ToString();
    CApCTRUtilities.m_registryManager.SetStringValue(valueName, str);
  }

  public static List<CApCTRSettingsTimerInfo> LoadInternalTimerSettings()
  {
    List<CApCTRSettingsTimerInfo> timerList = new List<CApCTRSettingsTimerInfo>();
    foreach (TxOlpCTRInternalEvent ctrInternalEvent in Enum.GetValues(typeof (TxOlpCTRInternalEvent)))
    {
      if (ctrInternalEvent <= 5)
        CApCTRUtilities.CreateSettingsTimer(ctrInternalEvent.ToString(), timerList);
    }
    return timerList;
  }

  private static void CreateSettingsTimer(string timerName, List<CApCTRSettingsTimerInfo> timerList)
  {
    CApCTRSettingsTimerInfo timerSetting = !TxOlpRoboticSimulationEventsManager.Instance.SupportPriority(timerName) ? new CApCTRSettingsTimerInfo(timerName) : (CApCTRSettingsTimerInfo) new CApCTRSettingsTimerInfoSupportPriority(timerName);
    CApCTRUtilities.UpdateSettingsTimerInfoFromRegistry(timerSetting);
    timerList.Add(timerSetting);
  }

  public static string NumericLinearFormat
  {
    get => "F" + TxApplication.Options.Units.LinearDigitsAfterDecimalPoint.ToString();
  }

  public static string DoubleToString(double val, string format, bool bTrimEndingZeros)
  {
    string str = val.ToString(format);
    if (NumberFormatInfo.CurrentInfo.NumberDecimalSeparator != ".")
      str = str.Replace(NumberFormatInfo.CurrentInfo.NumberDecimalSeparator, ".");
    if (bTrimEndingZeros)
    {
      if (str.Contains("."))
        str = str.TrimEnd('0');
      if (str.EndsWith("."))
        str = str.TrimEnd('.');
    }
    return str;
  }

  public static void AddSettingsTimerInfoToEventsManager(CApCTRSettingsTimerInfo setting)
  {
    TxOlpCTRSettingsTimerInfo settingsTimerInfo = !(setting is CApCTRSettingsTimerInfoSupportPriority) ? new TxOlpCTRSettingsTimerInfo(setting.TimerName, setting.TimerActive) : (TxOlpCTRSettingsTimerInfo) new TxOlpCTRSettingsTimerInfoSupportPriority(setting.TimerName, setting.TimerActive, (setting as CApCTRSettingsTimerInfoSupportPriority).TimerPriority);
    TxOlpRoboticSimulationEventsManager.Instance.AddTimerSetting(setting.TimerName, settingsTimerInfo);
  }

  internal static ResourceManager GetResourceManager()
  {
    if (CApCTRUtilities.m_resourceManager == null)
      CApCTRUtilities.m_resourceManager = new ResourceManager("DnProcessSimulateCommands.CycleTimeReport.CApCTRStringTable", Assembly.GetExecutingAssembly());
    return CApCTRUtilities.m_resourceManager;
  }
}
